<extend name="Layout:layout_edit" />
<block name="body_content_block">
    <form action="{:AddonU('Extension/extension_save')}" method="post" data-success-fn="show_reload_tip()">
        <table>
            <if condition="$isSuperAdmin">
                <th>租户ID：</th><td><select name="siteID" title="请选择租户" required data-url="{:AddonU('Admin/get_admin_json')}" data-value="{$data.siteID}"></select>
                <else/>
                <tr style="display: none;"><input type="hidden" name="siteID" value="{$loginData.siteID}"/></td></tr>
            </if>
            <tr>
                <th>分机号：</th><td>
            <if condition="$batch eq '1'">
                <input type="text" name="userName" placeholder="起始号码" size="8" data-check-url="{:AddonU('Status/cti_check_extension')}"/> 至
                <input type="text" name="userName2" placeholder="结束号码" size="8"/>
                <else/>
                <input type="text" name="userName" placeholder="用户名" value="{$data.userName}" title="请填写3-16个字符!" data-check-url="{:AddonU('Status/cti_check_extension')}"/>
            </if>
            <span style="color:#F00;" id="name-tip"></span>
            </td></tr>
            <tr>
                <th>显示号码：</th><td>
                    <input type="text" name="ctiCallerNo" placeholder="主叫号码" value="{$data.ctiCallerNo}" title="请填写主叫号码!"/>
                </td></tr>
            <tr>
                <th>分机密码：</th><td>
                    <input type="password" name="extensionPassword" placeholder="密码" title="请填写6-16个字符!"/><span>{$data.extensionPassword}</span>
                </td></tr>
            <tr>
                <th>注册端口：</th><td>
                    <input type="text" name="sipPort" placeholder="注册端口" value="{$data.sipPort}"/>
                </td></tr>
            <tr>
                <th>选择中继：</th><td>
                    <select name="ctiTrunk" title="请选择中继名称" data-url="{:AddonU('Trunk/get_json')}" data-value="{$data.ctiTrunk}"></select>
                </td></tr>
            <tr>
                <th>录音标识：</th><td>
                    <select name="recState" title="请选择录音标识" required>
                        <option value="1">录音</option>
                        <option value="0">不录音</option>
                    </select>
                </td></tr>
            <tr>
                <th>dtmfmode：</th><td>
                    <select name="dtmfmode" title="请选择dtmfmode">
                        <option value="rfc2833" selected="selected">RFC 2833</option>
                        <option value="inband">In band audio</option>
                        <option value="auto">Auto</option>
                        <option value="info">SIP INFO (application/dtmf-relay)</option>
                        <option value="shortinfo">SIP INFO (application/dtmf)</option>
                    </select>
                </td></tr>
            <tr>
                <th>canreinvite：</th><td>
                    <select name="canreinvite">
                        <option value="no">No</option>
                        <option value="yes">Yes</option>
                        <option value="nonat">nonat</option>
                        <option value="update" selected="selected">update</option>
                    </select>
                </td></tr>
            <tr>
                <th>dialopts：</th><td>
                    <input type="text" name="dialopts" id="dialopts" value="{$data.dialopts}"> <input type="checkbox" name="dialopts_cb" id="dialopts_cb" value="1">Override
                </td></tr>
            <tr>
                <th>状态：</th><td>
                    <input type="radio" name="state" value="1" checked="checked" required/>正常
                    <input type="radio" name="state" value="0" required/>禁用
                </td></tr>
        </table>
        <input type="hidden" name="ctiRegMode" value="{$data.ctiRegMode}"/>
        <input type='hidden' name='batch' value='{$batch}'/>
        <input type='hidden' name='id' value='{$data.id}'/>
    </form>
</block>
<block name="body_after_block">
    <script>
        item_form.resize_owner_dialog({width: 540});
        // 设置主键控件状态
        if ('' != '{$data.userName}') {
            $('input[name="extensionPassword"]').val('{$data.extensionPassword}').next().html(' ({$data.extensionPassword})');
            $('input[name="userName"]').attr("readonly", true);
        } else {
            $('input[name="userName"]').blur(function () {
                var $this = $(this);
                if (!$this.val()) {
                    return;
                }
                $.get($('input[name="userName"]').data('checkUrl'), {"extension": $this.val()}, function (data) {
                    if (data.status) {
                        $('#name-tip').text('');
                    } else {
                        $('#name-tip').text(data.info);
                        $this.val('');
                    }
                })
            })
        }
        $('select[name="dtmfmode"] option[value="{$data.dtmfmode}"]').prop('selected', true);
        $('select[name="canreinvite"] option[value="{$data.canreinvite}"]').prop('selected', true);
        $('select[name="recState"] option[value="{$data.recState}"]').prop('selected', 'selected');
        $('input[name="state"][value="{$data.state}"]').prop('checked', true);
        $('#dialopts_cb[value="{$data.dialopts_cb}"]').prop('checked', true);

        ajax_load_options({selector:'select[name="siteID"]', def_option: '选择租户', func: afterSiteID});
        afterSiteID();
        function afterSiteID() {
            var val = $('select[name="siteID"]').val();
            if (!val) {
                val = '{$loginData.siteID}';
            }
            ajax_load_options({selector: 'select[name="ctiTrunk"]', queryParams: {siteID: val}, def_option: '请选择中继'});
        }
        $('select[name="siteID"]').change(function () {
            afterSiteID();
        })
        $('#dialopts_cb').change(function () {
            // Tttg
            set_dialopts();
        })
        set_dialopts();
        function set_dialopts() {
            var $cb = $('#dialopts_cb');
            if ($cb.prop('checked')) {
                $('#dialopts').val('{$data.dialopts}').prop('disabled', false);
            } else {
                $('#dialopts').val('Tttg').prop('disabled', true);
            }
        }
        // 主叫号码:注册模式不可编辑，默认分机号 ip模式可编辑
        $('select[name="ctiTrunk"]').change(function () {
            var v = $(this).val();
            var txt = $(this).parent().find('option[value="' + v + '"]').text();
            if (txt.indexOf('(ip') > -1) {
                $('input[name="ctiRegMode"]').val('ip');
                // 2015-2-2 不判断了 $('input[name="ctiCallerNo"]').attr('readonly', false).val('');
            } else if (txt.indexOf('(register') > -1) {
                $('input[name="ctiRegMode"]').val('register');
                // 2015-2-2 不判断了 $('input[name="ctiCallerNo"]').attr('readonly', true).val($('input[name="userName"]').val());
            } else if (txt.indexOf('(ims') > -1) {
                $('input[name="ctiRegMode"]').val('ims');
            }
        })
    </script>
</block>